package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import dataprocess.IUESuperStation;
import dataprocess.IUEUavMonitor;
import dataprocess.IUEWind;
import utils.Config;
import utils.DbUtil;

public class JdbcDao {
    
    private static final Logger logger = LogManager.getLogger(JdbcDao.class);
    
    /**
     * 按照表中列的顺序依次赋值
     * @param conn
     * @param sql
     * @param params
     * @return
     */
    public static int insert(Connection conn, String sql, Object[] params){
        PreparedStatement pstmt = null;
        int result = 0;
        try{
            pstmt = conn.prepareStatement(sql);
            for(int i =0; i<params.length; i++){
                pstmt.setString(i+1, params[i]!=null?params[i].toString():null);
            }
            result = pstmt.executeUpdate();
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
        }finally{
            DbUtil.closeAll(pstmt,conn);
        }
        return result;
    }
    
    

    public static int insertUAVMonitorData(IUEUavMonitor uavData, Config config) {
        
        Object[] params = new Object[12];
        params[0]=uavData.getSid();
        params[1]=uavData.getCtime();
        params[2]=uavData.getTmp();
        params[3]=uavData.getHum();
        params[4]=uavData.getPm25();
        params[5]=uavData.getLng();
        params[6]=uavData.getLat();
        params[7]=uavData.getAsl();
        params[8]=uavData.getNp();
        params[9]=uavData.getSp();
        params[10]=uavData.getCo();
        params[11]=uavData.getOt();
        //NP,SP,CO,OT
        
        String sql = config.getSql();
        int result = 0;
        try {
            Connection conn = DbUtil.getConnection(config);
            insert(conn, sql, params);
        } catch (Exception e) {
            logger.error("error insertUAVMonitor: inserting , e= "+e);
        }
        return result;
    }
    
    public static int insertIUEWindData(IUEWind wind, Config config){
        Object[] params = new Object[2];
        //params[0] = wind.getSid();
        params[0] = wind.getWdir();
        params[1] = wind.getWspd();
        //params[3] = wind.getTimestamp();
        String sql = config.getSql();
        int result = 0;
        try {
            Connection conn = DbUtil.getConnection(config);
            insert(conn, sql, params);
        } catch (Exception e) {
            logger.error("error insertIUEWind: inserting , e= "+e);
        }
        return result;        
        
    }



    public static int insertIueSuperStation(IUESuperStation iss, Config config) {
        Object[] params = new Object[12];
        params[0] = iss.getSo2();
        params[1] = iss.getNo2();
        params[2] = iss.getPm10();
        params[3] = iss.getPm25();
        params[4] = iss.getCo();
        params[5] = iss.getO3();
        params[6] = iss.getWins();
        params[7] = iss.getWind();
        params[8] = iss.getTempr();
        params[9] = iss.getHum();
        params[10] = iss.getPress();
        params[11] = iss.getClear();
        //params[12] = iss.getTimestamp();
        System.out.println(iss);
        String sql = config.getSql();
        int result = 0;
        try {
            Connection conn = DbUtil.getConnection(config);
            insert(conn, sql, params);
        } catch (Exception e) {
            logger.error("error insertIUESuperStation: inserting , e= "+e);
        }
        return result;        
        
    }


}
